#include <iostream>
#include <algorithm>

using namespace std;

int p1[100860] = {}, p2[100860] = {};
int dp[2][100860] = {};

int main(int argc, char const *argv[])
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> p1[i];
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> p2[i];
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            int mx = 0;
            if (i != 1)
            {
                mx = max(mx, dp[(i - 1) % 2][j]);
            }
            if (j != 1)
            {
                mx = max(mx, dp[i % 2][j - 1]);
            }
            if (p1[i] == p2[j])
            {
                mx = max(mx, dp[(i - 1) % 2][j - 1] + 1);
            }
            dp[i % 2][j] = mx;
        }
    }
    cout << dp[n % 2][n];
    
    return 0;
}
